﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Configuration;

namespace Ferreteria.Clases
{
    class Funcion
    {
        public static decimal PrecioBonificado(decimal precio, decimal bonificacion)
        {
           
            return precio / (1 + bonificacion);
        }

        public static decimal? PrecioBonificado(decimal? precio, decimal? bonificacion)
        {

            return precio / (1 + bonificacion);
        }

        public static decimal SubtotDescuento(decimal precio, decimal bonificacion)
        {
            return precio - (precio * bonificacion);
            
        }

        public static decimal? SubtotDescuento(decimal? precio, decimal? bonificacion)
        {
            return precio - (precio * bonificacion);
            
        }




        public static Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

        public static byte[] imageToByteArray(System.Drawing.Image imageIn, string format)
        {
            MemoryStream ms = new MemoryStream();
            switch (format)
            {
                case "jpg":
                    {
                        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        return ms.ToArray();
                    }
                case "png":
                    {
                        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        return ms.ToArray();
                    }
                default:
                    {
                        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                        return ms.ToArray();
                    }
            }
        }
        
        public static Array obtenerTiposFactura(string condicionIVA)
        {
            switch (condicionIVA)
            {
                case "CF":
                        return new string[1] {"B" };
                case "EX":
                        return new string[1] { "B" };
                case "MT":
                        return new string[1] { "B" };
                case "NRI":
                        return new string[0] { };
                case "RI":
                        return new string[2] { "A" , "B" };
                default:
                        return new string[2] { "A", "B" };
            }
        }

        public static void ProtectSection(String sSectionName)
        {
            // Open the app.config file.
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // Get the section in the file.
            ConfigurationSection section = config.GetSection(sSectionName);
            // If the section exists and the section is not readonly, then protect the section.
            if (section != null)
            {
                if (!section.IsReadOnly())
                {
                    // Protect the section.
                    section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
                    section.SectionInformation.ForceSave = true;
                    // Save the change.
                    config.Save(ConfigurationSaveMode.Modified);
                }
            }
        }

        public static int obtenerAuth(string tipo)
        {
            var context = new DisancoEntities(Ferreteria.Clases.Constantes.cadenaConexion);

            foreach (var auth in context.Autorizaciones)
            {
                if (auth.autorizacion_tipo_comp == tipo && auth.autorizacion_estado && auth.autorizacion_limite_fecha.Date > DateTime.Now.Date)
                {
                    var numeroFactura = auth.autorizacion_ultimo_numero + 1;

                    if (numeroFactura > auth.autorizacion_limite_max)
                    {
                        return 0;
                    }
                    else
                    {
                        return auth.autorizacion_id;
                    }
                }
            }

            return 0;
        }

        public static decimal CalcularBonificacion(decimal bonAnt, decimal variacion)
        {
            return Math.Abs(((100 - bonAnt) + ((100 - bonAnt) * (variacion / 100))) - 100);
        }
    }
}
